package com.itheima.service;

import com.alibaba.druid.sql.PagerUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);

        Page<Permission> page = permissionDao.findPage(queryString);
        List<Permission> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, rows);
    }

    public Result add(Permission permission) {
        permissionDao.add(permission);
        return new Result(true, "添加成功");
    }

    public Permission findById(String id) {
        return permissionDao.findById(id);
    }

    public Result edit(Permission permission) {
        permissionDao.edit(permission);
        return new Result(true, "修改成功");
    }

    public Result delete(String id) {
        List<Integer> roleById = permissionDao.findRoleById(id);
        if (roleById.isEmpty()) {
            permissionDao.delete(id);
            return new Result(true, "删除成功");
        }
        return new Result(false, "该权限正被使用,请先解绑");
    }

    public List<Permission> findAll() {
        List<Permission> list = permissionDao.findAll();
        return list;
    }
}
